/**
 * Created by LangK on 2016/12/20.
 */
var path = require('path');
var http = require('http');
var express = require('express');
var proxyMiddleware = require('http-proxy-middleware')
require("./API/config.js")
var app = express()

app.use(express.static(path.join(__dirname, '/dist/')))
var port = 3034
app.set('port', port)

// var proxyA = require('socket.io-proxy')
// var socket = proxy.connect('http://192.168.1.69:20001')
// proxyA.init('http://192.168.1.69:20001')
// var socket = proxy.connect('http://192.168.1.69:20001')
// socket.on('connect', function () { 
//     console.log('Socket connected');
//     socket.on('command', function (data) { 
//         console.log('Received data');
//     });
//     socket.on('disconnect', function() { 
//         console.log('Socket disconnected');
//     });
// });

var proxyTable = {
  '/V1.0.0/account/manager': {
    target: "http://" + global.accountIp + ":" + global.accountport,
    changeOrigin: true
  },
  '/V1.0.0/accountRole': {
    target: "http://" + global.accountIp + ":" + global.accountport,
    changeOrigin: true
  },
  '/V1.0.0/role': {
    target: "http://" + global.accountIp + ":" + global.accountport,
    changeOrigin: true
  },
  '/V1.0.0/resource': {
    target: "http://" + global.accountIp + ":" + global.accountport,
    changeOrigin: true
  },
  '/V0.0.1/job': {
    target: "http://" + global.jobIp + ":" + global.jobport,
    changeOrigin: true
  },
  '/V0.0.1/task': {
    target: "http://" + global.jobIp + ":" + global.jobport,
    changeOrigin: true
  },
  '/socket.io': {
    target: "http://" + global.accountIp + ":" + global.scoketport,
    changeOrigin: true
  }
}
Object.keys(proxyTable).forEach(function (context) {
  var options = proxyTable[context]
  if (typeof options === 'string') {
    options = { target: options }
  }
  app.use(proxyMiddleware(context, options))
})


var server = http.createServer(app);

server.listen(port);
app.listen(8000, function () {
  console.log('server start')
})